package com.gxgeek.javabasic.base.integer;

import java.lang.reflect.Field;

/**
 * @author gaoxing
 * @create 2018-07-12 下午2:44
 */
public class Demo {
    public static void main(String[] args) throws Exception {

//        Integer a = 300, b =301;
        Integer a = 1, b =2;
        System.out.println("before a = "+a+" b="+b);
        swap(a, b);
        System.out.println("after a = "+a+" b="+b);
    }

    private static void swap(Integer a, Integer b) throws NoSuchFieldException, IllegalAccessException {
        Field field = Integer.class.getDeclaredField("value");
        field.setAccessible(true);

        int temp = a.intValue();//1
        field.setInt(a, b.intValue());//2
        field.setInt(b, temp);
//        field.set(a, b.intValue());//2
//        field.set(b, temp);
    }
}
